package com.lys.eduservice.service.impl;

import com.lys.eduservice.entity.EduChapter;
import com.lys.eduservice.entity.chapter.chapterVo;
import com.lys.eduservice.mapper.EduChapterMapper;
import com.lys.eduservice.mapper.EduVideoMapper;
import com.lys.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lys.exceptionHandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Lyyyys
 * @since 2021-04-30
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduChapterMapper eduChapterMapper;
    @Autowired
    private EduVideoMapper eduVideoMapper;


    //根据课程id查询，章节小节信息
    @Override
    public List<chapterVo> getChapterVideo(String id) {
        List<chapterVo> list = eduChapterMapper.getChapterVideo(id);
        return list;
    }

    //删除章节的方法，根据章节中有没小节，有小节就不删除
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据章节id，查询小节表
        int count = eduVideoMapper.getAllChapterCount(chapterId);
        //判断
        if(count > 0){//能查询出小节信息，不删除
            throw new GuliException(20001,"不能删除");
        }else {//不能查询出小节信息，就删除
            int result = eduChapterMapper.deleteById(chapterId);
            return result>0;
        }
    }

    @Override
    public Integer deleteChapterByCourseId(String id) {
        Integer result = eduChapterMapper.deleteChapterByCourseId(id);
        return result;
    }
}
